package com.vma.jifu.backsys.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.vma.jifu.bizModel.model.Staff;
import com.vma.jifu.bizModel.model.TgArticle;
import com.vma.jifu.bizModel.model.TgVedio;
import com.vma.tools.ReturnsUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by kitabi on 2017/6/16.
 */
public class PullController extends BaseController {

    public void qryImageList(){
        String title = getPara("title");

        int pageNo = getParaToInt("cPage");
        int pageSize = getParaToInt("pSize");
        String start = getPara("start");
        String end = getPara("end");

        List<String> param = new ArrayList<String>();
        String condition = "";

        if(title != null && !"".equals(title)){
            condition += "and title like ? ";
            param.add("%"+title+"%");
        }

        if(start != null && !"".equals(start)){
            condition += "and create_time > ? ";
            param.add(start);
        }
        if(end != null && !"".equals(end)){
            condition += "and create_time < ? ";
            param.add(end);
        }

        Page<TgArticle> list = TgArticle.dao.paginate(pageNo,pageSize,"select * ","from tg_article where status=1  "+condition + " order by id desc "
                ,param.toArray());

            renderJson(ReturnForm(list.getList(),list.getTotalRow()));//-1工号不存在



    }

    public void qryImageDetail(){
        String id =getPara("id");
        if(id != null && !"".equals(id)){
            TgArticle tgArticle = TgArticle.dao.findById(id);
            setAttr("initData",tgArticle);
        }

        renderVelocity("/newshop/artcle/image/addImage.html");
    }


    public void addImage(){
        String id =getPara("id");
        String title= getPara("title");
        String article_desc= getPara("article_desc");
        String img_url= getPara("img_url");
        String content= getPara("content");

        if(id ==null || "".equals(id)){
            TgArticle tg= new TgArticle();
            tg.setTitle(title);
            tg.setArticleDesc(article_desc);
            tg.setImgUrl(img_url);
            tg.setContent(content);
            tg.setCreateTime(new Date());
            tg.setStatus("1");
            tg.save();
            renderJson(ReturnsUtil.returnSuccess("新增成功"));
        }else{
            TgArticle tg= TgArticle.dao.findById(id);
            tg.setTitle(title);
            tg.setArticleDesc(article_desc);
            tg.setImgUrl(img_url);
            tg.setContent(content);
            tg.update();
            renderJson(ReturnsUtil.returnSuccess("修改成功"));
        }
    }


    public void qryVideoList(){
        String title = getPara("title");

        int pageNo = getParaToInt("cPage");
        int pageSize = getParaToInt("pSize");
        String start = getPara("start");
        String end = getPara("end");

        List<String> param = new ArrayList<String>();
        String condition = "";

        if(title != null && !"".equals(title)){
            condition += "and title like ? ";
            param.add("%"+title+"%");
        }

        if(start != null && !"".equals(start)){
            condition += "and create_time > ? ";
            param.add(start);
        }
        if(end != null && !"".equals(end)){
            condition += "and create_time < ? ";
            param.add(end);
        }

        Page<TgVedio> list = TgVedio.dao.paginate(pageNo,pageSize,"select * ","from tg_vedio where status=1 "+condition+ " order by id desc "
                ,param.toArray());

        renderJson(ReturnForm(list.getList(),list.getTotalRow()));//-1工号不存在



    }

    public void qryVideoDetail(){
        String id =getPara("id");
        if(id != null && !"".equals(id)){
            TgVedio tgVedio = TgVedio.dao.findById(id);
            setAttr("initData",tgVedio);
        }

        renderVelocity("/newshop/artcle/video/addVideo.html");
    }

    public void addVideo(){
        String id =getPara("id");
        String title= getPara("title");
        String article_desc= getPara("video_desc");
        String img_url= getPara("img_url");
        String video_url= getPara("video_url");


        if(id ==null || "".equals(id)){
            TgVedio tg= new TgVedio();
            tg.setTitle(title);
            tg.setVedioDesc(article_desc);
            tg.setImgUrl(img_url);
            tg.setVedioUrl(video_url);
            tg.setCreateTime(new Date());
            tg.setStatus(1);
            tg.save();
            renderJson(ReturnsUtil.returnSuccess("新增成功"));
        }else{
            TgVedio tg= TgVedio.dao.findById(id);
            tg.setTitle(title);
            tg.setVedioDesc(article_desc);
            tg.setImgUrl(img_url);
            tg.setVedioUrl(video_url);
            tg.update();
            renderJson(ReturnsUtil.returnSuccess("修改成功"));
        }
    }

    public void deleteImage(){
        String id = getPara("id");
        TgArticle tgArticle=TgArticle.dao.findById(id);
        tgArticle.setStatus("0");


        tgArticle.update();
        renderJson(ReturnsUtil.returnSuccess("更新成功"));
    }

    public void deleteVideo(){
        String id = getPara("id");
        TgVedio tgArticle=TgVedio.dao.findById(id);
        tgArticle.setStatus(0);


        tgArticle.update();
        renderJson(ReturnsUtil.returnSuccess("更新成功"));
    }
}
